<?php

namespace Maxlcoder\LaravelZwdd;

use Exception;
use Maxlcoder\LaravelZwdd\Util\BestSignHttpClient;

class Ssq
{
    protected $client;

    private $serverHost = "https://api.bestsign.cn";
    // 应用秘钥
    private $clientSecret;
    // RSA签名私钥
    // 注意：在上上签开放平台上生成的密钥对，其私钥格式为PKCS#8
    private $privateKey;
    private $clientId;
    private $httpClient;


    public function __construct()
    {
        $this->clientId = config('ssq.clientId');
        $this->clientSecret = config('ssq.clientSecret');
        $this->privateKey = config('ssq.privateKey');
        $this->httpClient = BestSignHttpClient::getInstance($this->serverHost, $this->clientId, $this->clientSecret, $this->privateKey);
    }

    /**
     * 获取 access_token
     */
    public function accessToken()
    {
        return $this->clientId;
    }
    public function bindingExistence($postData)
    {
        $path = '/api/users/binding-existence';
        $method = "POST";
        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }

    public function bindAccount($postData)
    {
        $path = '/api/users/binding-url';
        $method = "POST";
        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }


//--------------------- Util ---------------------//


    public function downloadContractAppendix($contractId, $account, $enterpriseName)
    {
        $path = "/api/contracts/{$contractId}/appendix-file";
        $method = "GET";

        $urlParams['account'] = $account;
        //中文需要encode
        $urlParams['enterpriseName'] = urlencode($enterpriseName);

        $response = $this->httpClient->request($path, $method, null, $urlParams);
        return $response;
    }

    public function queryBindingStatus($postData)
    {
        $path = "/api/users/binding-existence";
        $method = "POST";

        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }

    /**
     * 发送并且创建合同
     * @param $receivers
     */
    public function sendContract( $postData)
    {
        $path = "/api/templates/send-contracts-sync-v2";
        $method = "POST";
        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }

    /**
     * 待提交实名认证信息
     */
    public function personAuth( $postData)
    {
        try{
            $path = "/api/users/commit/person-auth/info";
            $method = "POST";

            $response = $this->httpClient->request($path, $method, $postData);
            return $response;
        }catch (Exception $exception){
            return false;
        }
    }

    public function downloadByOrder($postData)
    {
        $path = "/api/contracts/downloadByOrder";
        $method = "POST";
        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }

    public function revoke($contractId, $revokeReason, $postData)
    {
        $path = "/api/contracts/".$contractId."/revoke";
        $method = "POST";
        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }

    public function detail($contractId)
    {
        $path = "/api/contracts/detail/".$contractId;
        $method = "GET";

        $urlParams['enterpriseName'] = '';		//中文需要encode

        $response = $this->httpClient->request($path, $method, null, $urlParams);
        return $response;
    }

    public function link($postData)
    {
        $path = "/api/users/sso-link";
        $method = "POST";
        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }

    public function sign($postData)
    {
        $path = "/api/contracts/sign";
        $method = "POST";
        $response = $this->httpClient->request($path, $method, $postData);
        return $response;
    }

    public function templateDetail($id)
    {
        $path = "/api/templates/{$id}";
        $method = "GET";
        $response = $this->httpClient->request($path, $method, null, '');
        return $response;
    }
}
